import axios from '@/axios'
import useDealResult from '@/hooks/dealResult'
import api from '@/api/user'

const { dealResult } = useDealResult()
// 登录
export const loginByCloud = async (param: any): Promise<any | null> => {
  const [result, err] = await axios.post(api.login, param)
  return Promise.resolve(dealResult(err, result))
}
// 登录
export const loginByUserPassword = async (param: any): Promise<any | null> => {
  const [result, err] = await axios.post(api.login, param)
  return Promise.resolve(dealResult(err, result))
}
export const getCurrentUser = async (): Promise<any | null> => {
  const [result, err] = await axios.post(api.currentUserInfo)
  return Promise.resolve(dealResult(err, result))
}
export const getUserMenuList = async (): Promise<any | null> => {
  const [result, err] = await axios.post(api.appMenuResource)
  return Promise.resolve(
    dealResult(err, result, (data: any) => {
      const resourceGroupList = data.data.result.resourceGroupList.map((el:any) => el.resourceGroupCode)
      data.data.result.menuList.forEach((item: any) => {
        resourceGroupList.push(item.menuCode)
      })
      return {
        menuList: data.data.result.menuList,
        resourceGroupList,
      }
    }),
  )
}
